﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：GetPatientResuorce                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/4/20 14:59:26                        
*└────────────────────────────────────────────────┘
*/

using Dapper;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Data
{
    public class PatientResuorce
    {
        private static PatientResuorce m_patientResuorce = null;

        private PatientResuorce()
        {

        }

        public static PatientResuorce patientResuorce
        {
            get
            {
                if (m_patientResuorce == null)
                    m_patientResuorce = new PatientResuorce();
                return m_patientResuorce;
            }
        }


        /// <summary>
        /// 获取转院的调度患者信息
        /// </summary>
        /// <returns></returns>
        public string GetZYDisPatientInfo1(string taskCode, CarInfo car, string ZDCode, int stateId)
        {
            string json = string.Empty;
            try
            {
                Hashtable ht = new Hashtable();
                StringBuilder sb = new StringBuilder(@" select  tt.任务编码 as missionId, tac.开始受理时刻 as callTime ,tac.现场地址 as patientAddress,tac.主诉 as complaint,
                                    tt.出车时刻 as startTime, tt.到达现场时刻 as arrivalTime,tt.离开现场时刻 as leaveArrivalTime,tt.到达医院时刻 as toHospTime,tt.完成时刻 as compHandTime,
                                    ta.车牌号码 as carNo,ta.车辆编码 as carId,tp.工号,tp.姓名,tp.通话号码
                                    from TTask  tt
                                    left join TAmbulance ta  on tt.车辆编码 = ta.车辆编码
                                    left join TAcceptEvent tac on tt.事件编码=tac.事件编码 and tt.受理序号=tac.受理序号
                                    left join TPerson tp on tt.车辆编码=tp.绑定车辆编码 and tp.类型编码=4
                                    where tt.是否执行中=1 and tt.任务编码='" + taskCode + "'");
                DataSet ds = DataSetUtility.GetDataSetFromMain(sb);

                //获取社区患者信息
                StringBuilder sb1 = new StringBuilder(@" select cp.guid,cp.commuPatientId,cp.jgdm,cp.mzh,cp.toHospital,cp.timiStartTime,cp.timiEndTime,cp.timiStartAgreeTime,cp.timiSignAgreeTime,cp.toHospital,
                                    pt.name,pt.gender,pt.age,pt.telecom,pt.nation,pt.idCard,pt.marital,pt.occCode,pt.medIns,pt.present,pt.past,pt.allergic,pt.operation,pt.family,pt.medicen
                                    ,phy.examTime,phy.spo2,phy.aware,phy.temp,phy.pulse,phy.rhythm,phy.breath,phy.diastolic,phy.systolic,phy.pupilLeft,phy.pupilRight,phy.inspect,phy.test,phy.diagnosis,phy.avg,phy.low,phy.high
                                     ,cp.troponinResultTime,cp.troponinResult,cp.treatTime,cp.medicine,cp.dosage,cp.onsetTimeImprecise,cp.onsetTimeZone
									 ,cp.patientAddrProv,cp.patientAddrCity,cp.patientAddrCounty,cp.patientAddrStreet,cp.patientAddrCommunity,cp.patientAddressDetail
									 ,cp.timiCheck,cp.isDoTimi,cp.directToPlace,cp.doTimiPlace,cp.timiRe,cp.afterTimiAngioTime
									 ,pt.birthday,pt.idCardType,pt.eduLevCode,pt.medInsType,pt.medInsHeavy,pt.height,pt.weight
                                    from  CommuPatientInfoWD  cp
                                    left join PatientInfoWD  pt on pt.guid=cp.guid and pt.infoType=2
                                    left join PhyInfoWD  phy  on phy.guid=cp.guid and phy.infoType=2 where cp.guid='" + ZDCode + "'");
                DataSet ds1 = DataSetUtility.GetDataSetFromTold(sb1);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DisPatientInfo dis = new DisPatientInfo();
                    DataRow dr = ds.Tables[0].Rows[0];


                    PatientInfoWD patient = new PatientInfoWD();
                    PhyInfoWD phyInfo = new PhyInfoWD();
                    BloodPress blood = new BloodPress();

                    dis.missionId = dr["missionId"].ToString();
                    dis.complaint = dr["complaint"].ToString();
                    dis.callTime = DBConvert.DBTypeToDateTime(dr["callTime"]);
                    dis.patientAddress = dr["patientAddress"].ToString();

                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[0];
                        patient.name = dr1["name"].ToString();
                        patient.gender = dr1["gender"].ToString();
                        patient.age = dr1["age"].ToString();
                        patient.telecom = dr1["telecom"].ToString();
                        patient.idCard = dr1["idCard"].ToString();

                        //生命体征数据
                        phyInfo.spo2 = dr1["spo2"].ToString();

                        string aware = dr1["aware"].ToString();
                        if (aware.Equals("清醒"))
                        {
                            phyInfo.aware = "A";
                        }
                        else if (aware.Equals("对语言有反应"))
                        {
                            phyInfo.aware = "V";
                        }
                        else if (aware.Equals("对刺痛有反应"))
                        {
                            phyInfo.aware = "P";
                        }
                        else if (aware.Equals("对任何刺激都无反应"))
                        {
                            phyInfo.aware = "U";
                        }
                        else
                        {
                            phyInfo.aware = aware;
                        }

                        phyInfo.temp = dr1["temp"].ToString();
                        phyInfo.pulse = dr1["pulse"].ToString();
                        phyInfo.rhythm = dr1["rhythm"].ToString();
                        phyInfo.breath = dr1["breath"].ToString();
                        blood.diastolic = dr1["diastolic"].ToString();
                        blood.systolic = dr1["systolic"].ToString();
                        phyInfo.bloodPress = blood;
                        car.toHospital = dr1["toHospital"].ToString().Trim();

                        dis.timiStartAgreeTime = dr1["timiStartAgreeTime"].ToString();
                        dis.timiSignAgreeTime = dr1["timiSignAgreeTime"].ToString();
                        dis.timiStartTime = dr1["timiStartTime"].ToString();
                        dis.timiEndTime = dr1["timiEndTime"].ToString();
                        dis.troponinResultTime = dr1["troponinResultTime"].ToString();
                        dis.troponinResult = dr1["troponinResult"].ToString();

                        car.pickHospital = dr1["jgdm"].ToString();

                        //2021.07.06新增门诊号 
                        dis.MZH = dr1["mzh"].ToString();
                        dis.fstHospConTime = dr1["treatTime"].ToString();
                        dis.medicine = dr1["medicine"].ToString();
                        dis.dosage = dr1["dosage"].ToString();

                        //2021.07.21新增
                        dis.onsetTimeImprecise = dr1["onsetTimeImprecise"].ToString();
                        dis.onsetTimeZone = dr1["onsetTimeZone"].ToString();
                        dis.patientAddr.patientAddrProv = dr1["patientAddrProv"].ToString();
                        dis.patientAddr.patientAddrCity = dr1["patientAddrCity"].ToString();
                        dis.patientAddr.patientAddrCounty = dr1["patientAddrCounty"].ToString();
                        dis.patientAddr.patientAddrStreet = dr1["patientAddrStreet"].ToString();
                        dis.patientAddr.patientAddrCommunity = dr1["patientAddrCommunity"].ToString();
                        dis.patientAddr.patientAddressDetail = dr1["patientAddressDetail"].ToString();
                        dis.timiCheck = dr1["timiCheck"].ToString();
                        dis.isDoTimi = dr1["isDoTimi"].ToString();
                        dis.directToPlace = dr1["directToPlace"].ToString();
                        dis.doTimiPlace = dr1["doTimiPlace"].ToString();
                        dis.timiRe = dr1["timiRe"].ToString();
                        dis.afterTimiAngioTime = dr1["afterTimiAngioTime"].ToString();

                        patient.birthday = dr1["birthday"].ToString();
                        patient.idCardType = dr1["idCardType"].ToString();
                        patient.eduLevCode = dr1["eduLevCode"].ToString();
                        patient.medInsType = dr1["medInsType"].ToString();
                        patient.medInsHeavy = dr1["medInsHeavy"].ToString();
                        patient.height = dr1["height"].ToString();
                        patient.weight = dr1["weight"].ToString();

                    }

                    car.carNo = dr["carNo"].ToString();
                    car.carId = dr["carId"].ToString();
                    car.doctor = dr["姓名"].ToString();
                    car.telphone = dr["通话号码"].ToString();
                    //离开现场时间
                    if (!string.IsNullOrEmpty(dr["leaveArrivalTime"].ToString()))
                    {
                        car.leaveArrivalTime = Convert.ToDateTime(dr["leaveArrivalTime"].ToString());
                    }

                    string gonghao = dr["工号"].ToString();
                    //获取设备号
                    CarInfo deviceInfo = DapperData.GetDeviceInfo(gonghao);
                    if (deviceInfo == null)
                    {
                        deviceInfo = new CarInfo();
                    }
                    car.equipCode = deviceInfo.equipCode;
                    car.equipType = deviceInfo.equipType;

                    if (!string.IsNullOrEmpty(dr["startTime"].ToString()))
                    {
                        car.startTime = Convert.ToDateTime(dr["startTime"].ToString());
                    }

                    if (!string.IsNullOrEmpty(dr["arrivalTime"].ToString()))
                    {
                        car.arrivalTime = Convert.ToDateTime(dr["arrivalTime"].ToString());
                    }

                    if (!string.IsNullOrEmpty(dr["toHospTime"].ToString()))
                    {
                        car.toHospTime = Convert.ToDateTime(dr["toHospTime"].ToString());
                    }

                    if (!string.IsNullOrEmpty(dr["compHandTime"].ToString()))
                    {
                        car.compHandTime = Convert.ToDateTime(dr["compHandTime"].ToString());
                    }

                    if (stateId == 2)
                    {
                        //获取基层医院坐标
                        TOrganization pickHosp = DapperData.GetHospInfo(car.pickHospital);
                        if (pickHosp == null || string.IsNullOrEmpty(car.X) || string.IsNullOrEmpty(car.Y))
                        {
                            car.pickEstiTime = "未获取到坐标，无法计算距离和时间";
                            car.pickEstiDist = "未获取到坐标，无法计算距离和时间";
                        }
                        else
                        {

                            double[] bdArray = CoorConvert.Gps84ToBd09(Convert.ToDouble(car.Y), Convert.ToDouble(car.X));//gps转百度坐标
                            string start = bdArray[0] + "," + bdArray[1];//救护车的纬度和经度凭据
                            //string start = car.Y + "," + car.X;//救护车的经纬度
                            string end = pickHosp.latitude + "," + pickHosp.longitude;//医院的经纬度
                            //百度算路
                            string postresult = AppConfig.BDApi + "?tactics=11&output=json&origins=" + end + "&destinations=" + start + "&ak=" + AppConfig.BDKey + "&coord_type=wgs84";
                            string result = HttpService.SendDataByGET(postresult);
                            if (!string.IsNullOrEmpty(result))
                            {
                                BaiduInfo baiduInfo = JsonHelper.DeserializeObject<BaiduInfo>(result);
                                if (baiduInfo.status == "0")
                                {
                                    //预估距离
                                    string distance = baiduInfo.result.Select(x => x.distance).FirstOrDefault()?.text;
                                    //预估时间
                                    string duration = baiduInfo.result.Select(x => x.duration).FirstOrDefault()?.text;
                                    car.pickEstiDist = distance;
                                    car.pickEstiTime = duration;
                                }
                                else
                                {
                                    LogUtility.Error("PatientResuorce/GetDisPatientInfo   中百度算路出错", baiduInfo.message);
                                }
                            }
                        }
                    }

                    if (stateId == 4)
                    {
                        //获取医院坐标
                        TOrganization hospInfo = DapperData.GetHospInfo(car.toHospital);
                        if (hospInfo == null)
                        {
                            hospInfo = new TOrganization();
                        }
                        car.hospX = hospInfo.longitude;
                        car.hospY = hospInfo.latitude;

                        if (string.IsNullOrEmpty(car.X) || string.IsNullOrEmpty(car.Y) || string.IsNullOrEmpty(car.hospX) || string.IsNullOrEmpty(car.hospY))
                        {
                            car.estiDist = "未获取到坐标，无法计算距离和时间";
                            car.estiTime = "未获取到坐标，无法计算距离和时间";
                        }
                        else
                        {
                            double[] bdArray = CoorConvert.Gps84ToBd09(Convert.ToDouble(car.Y), Convert.ToDouble(car.X));//gps转百度坐标
                            string start = bdArray[0] + "," + bdArray[1];//救护车的纬度和经度凭据
                            //string start = car.Y + "," + car.X;//救护车的经纬度
                            string end = car.hospY + "," + car.hospX;//医院的经纬度
                            //百度算路
                            string postresult = AppConfig.BDApi + "?tactics=11&output=json&origins=" + end + "&destinations=" + start + "&ak=" + AppConfig.BDKey + "&coord_type=wgs84";
                            string result = HttpService.SendDataByGET(postresult);
                            if (!string.IsNullOrEmpty(result))
                            {
                                BaiduInfo baiduInfo = JsonHelper.DeserializeObject<BaiduInfo>(result);
                                if (baiduInfo.status == "0")
                                {
                                    //预估距离
                                    string distance = baiduInfo.result.Select(x => x.distance).FirstOrDefault()?.text;
                                    //预估时间
                                    string duration = baiduInfo.result.Select(x => x.duration).FirstOrDefault()?.text;
                                    car.estiDist = distance;
                                    car.estiTime = duration;
                                }
                                else
                                {
                                    LogUtility.Error("PatientResuorce/GetDisPatientInfo   中百度算路出错", baiduInfo.message);
                                }
                            }
                        }
                    }

                    if (stateId == 5)
                    {
                        if (deviceInfo.equipType == "卓尔")
                        {
                            EquipDataWD equipData = ToldDapperData.GetEquipData(taskCode);
                            if (equipData != null)
                            {
                                phyInfo.spo2 = equipData.oxygen;
                                phyInfo.pulse = equipData.pulse;
                                phyInfo.rhythm = equipData.heartRate;
                                blood.diastolic = equipData.low;
                                blood.systolic = equipData.high;
                                phyInfo.bloodPress = blood;
                            }
                        }

                    }

                    dis.patientInfo = patient;
                    dis.phyInfo = phyInfo;
                    dis.carInfo = car;
                    //LogUtility.Debug("PatientResuorce/GetDisPatientInfo", "调度数据  " + JsonHelper.SerializeObject(dis));
                    ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    ht.Add("msgId", Guid.NewGuid().ToString());
                    ht.Add("vendorId", "ANKE");
                    ht.Add("disPatientInfo", dis);
                    json = JsonHelper.SerializeObject(ht);
                    LogUtility.Debug("PatientResuorce/GetDisPatientInfo", "调度数据  " + json);
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("PatientResuorce/GetDisPatientInfo", ex.ToString());
            }
            return json;

        }


        #region 注释

        ///// <summary>
        ///// 获取转院的调度患者信息
        ///// </summary>
        ///// <returns></returns>
        //public string GetZYDisPatientInfo1(string taskCode, CarInfo car, string ZDCode, int stateId)
        //{
        //    string json = string.Empty;
        //    try
        //    {
        //        Hashtable ht = new Hashtable();
        //        StringBuilder sb = new StringBuilder(@" select  tt.任务编码 as missionId, tac.开始受理时刻 as callTime ,tac.现场地址 as patientAddress,tac.主诉 as complaint,
        //                            tt.出车时刻 as startTime, tt.到达现场时刻 as arrivalTime,tt.离开现场时刻 as leaveArrivalTime,tt.到达医院时刻 as toHospTime,tt.完成时刻 as compHandTime,
        //                            ta.车牌号码 as carNo,ta.车辆编码 as carId,tp.工号,tp.姓名,tp.通话号码
        //                            from TTask  tt
        //                            left join TAmbulance ta  on tt.车辆编码 = ta.车辆编码
        //                            left join TAcceptEvent tac on tt.事件编码=tac.事件编码 and tt.受理序号=tac.受理序号
        //                            left join TPerson tp on tt.车辆编码=tp.绑定车辆编码 and tp.类型编码=4
        //                            where tt.是否执行中=1 and tt.任务编码='" + taskCode + "'");
        //        DataSet ds = DataSetUtility.GetDataSetFromMain(sb);

        //        //获取社区患者信息
        //        StringBuilder sb1 = new StringBuilder(@" select cp.guid,cp.commuPatientId,cp.jgdm,cp.mzh,cp.toHospital,cp.timiStartTime,cp.timiEndTime,cp.timiStartAgreeTime,cp.timiSignAgreeTime,cp.toHospital,
        //                            pt.name,pt.gender,pt.age,pt.telecom,pt.nation,pt.idCard,pt.marital,pt.occCode,pt.medIns,pt.present,pt.past,pt.allergic,pt.operation,pt.family,pt.medicen
        //                            ,phy.examTime,phy.spo2,phy.aware,phy.temp,phy.pulse,phy.rhythm,phy.breath,phy.diastolic,phy.systolic,phy.pupilLeft,phy.pupilRight,phy.inspect,phy.test,phy.diagnosis,phy.avg,phy.low,phy.high
        //                             ,cp.troponinResultTime,cp.troponinResult
        //                            from  CommuPatientInfoWD  cp
        //                            left join PatientInfoWD  pt on pt.guid=cp.guid and pt.infoType=2
        //                            left join PhyInfoWD  phy  on phy.guid=cp.guid and phy.infoType=2 where cp.guid='" + ZDCode + "'");
        //        DataSet ds1 = DataSetUtility.GetDataSetFromTold(sb1);

        //        if (ds.Tables[0].Rows.Count > 0)
        //        {
        //            DisPatientInfo dis = new DisPatientInfo();
        //            DataRow dr = ds.Tables[0].Rows[0];


        //            PatientInfoWD patient = new PatientInfoWD();
        //            PhyInfoWD phyInfo = new PhyInfoWD();
        //            BloodPress blood = new BloodPress();

        //            dis.missionId = dr["missionId"].ToString();
        //            dis.complaint = dr["complaint"].ToString();
        //            dis.callTime = DBConvert.DBTypeToDateTime(dr["callTime"]);
        //            dis.patientAddress = dr["patientAddress"].ToString();

        //            if (ds1.Tables[0].Rows.Count > 0)
        //            {
        //                DataRow dr1 = ds1.Tables[0].Rows[0];
        //                patient.name = dr1["name"].ToString();
        //                patient.gender = dr1["gender"].ToString();
        //                patient.age = dr1["age"].ToString();
        //                patient.telecom = dr1["telecom"].ToString();
        //                patient.idCard = dr1["idCard"].ToString();

        //                //生命体征数据
        //                phyInfo.spo2 = dr1["spo2"].ToString();
        //                phyInfo.aware = dr1["aware"].ToString();
        //                phyInfo.temp = dr1["temp"].ToString();
        //                phyInfo.pulse = dr1["pulse"].ToString();
        //                phyInfo.rhythm = dr1["rhythm"].ToString();
        //                phyInfo.breath = dr1["breath"].ToString();
        //                blood.diastolic = dr1["diastolic"].ToString();
        //                blood.systolic = dr1["systolic"].ToString();
        //                phyInfo.bloodPress = blood;
        //                car.toHospital = dr1["toHospital"].ToString().Trim();

        //                dis.timiStartAgreeTime = dr1["timiStartAgreeTime"].ToString();
        //                dis.timiSignAgreeTime = dr1["timiSignAgreeTime"].ToString();
        //                dis.timiStartTime = dr1["timiStartTime"].ToString();
        //                dis.timiEndTime = dr1["timiEndTime"].ToString();
        //                dis.troponinResultTime = dr1["troponinResultTime"].ToString();
        //                dis.troponinResult = dr1["troponinResult"].ToString();

        //                car.pickHospital = dr1["jgdm"].ToString();

        //                //2021.07.06新增门诊号 
        //                dis.MZH = dr1["mzh"].ToString();

        //            }

        //            car.carNo = dr["carNo"].ToString();
        //            car.carId = dr["carId"].ToString();
        //            car.doctor = dr["姓名"].ToString();
        //            car.telphone = dr["通话号码"].ToString();
        //            //离开现场时间
        //            if (!string.IsNullOrEmpty(dr["leaveArrivalTime"].ToString()))
        //            {
        //                car.leaveArrivalTime = Convert.ToDateTime(dr["leaveArrivalTime"].ToString());
        //            }

        //            string gonghao = dr["工号"].ToString();
        //            //获取设备号
        //            CarInfo deviceInfo = DapperData.GetDeviceInfo(gonghao);
        //            if (deviceInfo == null)
        //            {
        //                deviceInfo = new CarInfo();
        //            }
        //            car.equipCode = deviceInfo.equipCode;
        //            car.equipType = deviceInfo.equipType;

        //            if (!string.IsNullOrEmpty(dr["startTime"].ToString()))
        //            {
        //                car.startTime = Convert.ToDateTime(dr["startTime"].ToString());
        //            }

        //            if (!string.IsNullOrEmpty(dr["arrivalTime"].ToString()))
        //            {
        //                car.arrivalTime = Convert.ToDateTime(dr["arrivalTime"].ToString());
        //            }

        //            if (!string.IsNullOrEmpty(dr["toHospTime"].ToString()))
        //            {
        //                car.toHospTime = Convert.ToDateTime(dr["toHospTime"].ToString());
        //            }

        //            if (!string.IsNullOrEmpty(dr["compHandTime"].ToString()))
        //            {
        //                car.compHandTime = Convert.ToDateTime(dr["compHandTime"].ToString());
        //            }

        //            if (stateId == 2)
        //            {
        //                //获取基层医院坐标
        //                TOrganization pickHosp = DapperData.GetHospInfo(car.pickHospital);
        //                if (pickHosp == null || string.IsNullOrEmpty(car.X) || string.IsNullOrEmpty(car.Y))
        //                {
        //                    car.pickEstiTime = "未获取到坐标，无法计算距离和时间";
        //                    car.pickEstiDist = "未获取到坐标，无法计算距离和时间";
        //                }
        //                else
        //                {

        //                    double[] bdArray = CoorConvert.Gps84ToBd09(Convert.ToDouble(car.Y), Convert.ToDouble(car.X));//gps转百度坐标
        //                    string start = bdArray[0] + "," + bdArray[1];//救护车的纬度和经度凭据
        //                    //string start = car.Y + "," + car.X;//救护车的经纬度
        //                    string end = pickHosp.latitude + "," + pickHosp.longitude;//医院的经纬度
        //                    //百度算路
        //                    string postresult = AppConfig.BDApi + "?tactics=11&output=json&origins=" + end + "&destinations=" + start + "&ak=" + AppConfig.BDKey + "&coord_type=wgs84";
        //                    string result = HttpService.SendDataByGET(postresult);
        //                    if (!string.IsNullOrEmpty(result))
        //                    {
        //                        BaiduInfo baiduInfo = JsonHelper.DeserializeObject<BaiduInfo>(result);
        //                        if (baiduInfo.status == "0")
        //                        {
        //                            //预估距离
        //                            string distance = baiduInfo.result.Select(x => x.distance).FirstOrDefault()?.text;
        //                            //预估时间
        //                            string duration = baiduInfo.result.Select(x => x.duration).FirstOrDefault()?.text;
        //                            car.pickEstiDist = distance;
        //                            car.pickEstiTime = duration;
        //                        }
        //                        else
        //                        {
        //                            LogUtility.Error("PatientResuorce/GetDisPatientInfo   中百度算路出错", baiduInfo.message);
        //                        }
        //                    }
        //                }
        //            }

        //            if (stateId == 4)
        //            {
        //                //获取医院坐标
        //                TOrganization hospInfo = DapperData.GetHospInfo(car.toHospital);
        //                if (hospInfo == null)
        //                {
        //                    hospInfo = new TOrganization();
        //                }
        //                car.hospX = hospInfo.longitude;
        //                car.hospY = hospInfo.latitude;

        //                if (string.IsNullOrEmpty(car.X) || string.IsNullOrEmpty(car.Y) || string.IsNullOrEmpty(car.hospX) || string.IsNullOrEmpty(car.hospY))
        //                {
        //                    car.estiDist = "未获取到坐标，无法计算距离和时间";
        //                    car.estiTime = "未获取到坐标，无法计算距离和时间";
        //                }
        //                else
        //                {
        //                    double[] bdArray = CoorConvert.Gps84ToBd09(Convert.ToDouble(car.Y), Convert.ToDouble(car.X));//gps转百度坐标
        //                    string start = bdArray[0] + "," + bdArray[1];//救护车的纬度和经度凭据
        //                    //string start = car.Y + "," + car.X;//救护车的经纬度
        //                    string end = car.hospY + "," + car.hospX;//医院的经纬度
        //                    //百度算路
        //                    string postresult = AppConfig.BDApi + "?tactics=11&output=json&origins=" + end + "&destinations=" + start + "&ak=" + AppConfig.BDKey + "&coord_type=wgs84";
        //                    string result = HttpService.SendDataByGET(postresult);
        //                    if (!string.IsNullOrEmpty(result))
        //                    {
        //                        BaiduInfo baiduInfo = JsonHelper.DeserializeObject<BaiduInfo>(result);
        //                        if (baiduInfo.status == "0")
        //                        {
        //                            //预估距离
        //                            string distance = baiduInfo.result.Select(x => x.distance).FirstOrDefault()?.text;
        //                            //预估时间
        //                            string duration = baiduInfo.result.Select(x => x.duration).FirstOrDefault()?.text;
        //                            car.estiDist = distance;
        //                            car.estiTime = duration;
        //                        }
        //                        else
        //                        {
        //                            LogUtility.Error("PatientResuorce/GetDisPatientInfo   中百度算路出错", baiduInfo.message);
        //                        }
        //                    }
        //                }
        //            }


        //            //if (stateId == 5) {
        //            //    //if (deviceInfo.equipType=="卓尔") {
        //            //    //    ToldDapperData.GetEquipData()
        //            //    //}

        //            //}

        //            dis.patientInfo = patient;
        //            dis.phyInfo = phyInfo;
        //            dis.carInfo = car;
        //            //LogUtility.Debug("PatientResuorce/GetDisPatientInfo", "调度数据  " + JsonHelper.SerializeObject(dis));
        //            ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        //            ht.Add("msgId", Guid.NewGuid().ToString());
        //            ht.Add("vendorId", "ANKE");
        //            ht.Add("disPatientInfo", dis);
        //            json = JsonHelper.SerializeObject(ht);
        //            LogUtility.Debug("PatientResuorce/GetDisPatientInfo", "调度数据  " + json);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogUtility.Error("PatientResuorce/GetDisPatientInfo", ex.ToString());
        //    }
        //    return json;

        //}
        #endregion



        /// <summary>
        /// 获取调度患者信息(120专线)
        /// </summary>
        /// <returns></returns>
        public string GetDisPatientInfo(string strPKCode)
        {
            string json = string.Empty;
            try
            {
                Hashtable ht = new Hashtable();
                StringBuilder sb = new StringBuilder(@" 
                                   SELECT  tt.告知单编码 AS toldCode, tt.任务编码 AS missionId,tt.预计到达时间,tt.预估距离,
                                  ts.血氧饱和度 AS spo2, ts.体温 AS temp, ts.脉搏 AS pulse, ts.心率 AS rhythm, ts.舒张压 AS diastolic, ts.收缩压 AS systolic, 
                                  tor.机构编码 AS toHospital, tor.经度 AS hospX, tor.纬度 AS hospY,tzd.名称 as ShenZhi
		                          FROM TToldInfo AS tt 
                                  LEFT  JOIN TSMTZ AS ts ON tt.告知单编码 = ts.告知单编码 
                                  LEFT JOIN TOrganization AS tor ON tt.送往医院编码 = tor.机构编码 
                                  left join TChestPain tc  on tt.告知单编码 = tc.告知单编码 
								  left join TZDictionary  tzd on tzd.编码=tc.神志 and tzd.类型名称='ShenZhi' 
				                  where tt.告知单编码='" + strPKCode + "'");
                //StringBuilder sb = new StringBuilder(@" select  *  from  DisPatientInfoView where toldCode='" + strPKCode + "'");
                DataSet ds = DataSetUtility.GetDataSetFromTold(sb);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DisPatientInfo dis = new DisPatientInfo();
                    DataRow dr = ds.Tables[0].Rows[0];
                    PatientInfoWD patient = new PatientInfoWD();
                    CarInfo car = new CarInfo();
                    PhyInfoWD phyInfo = new PhyInfoWD();
                    BloodPress blood = new BloodPress();

                    //获取任务编码
                    string taskCode = dr["missionId"].ToString();
                    dis.missionId = taskCode;
                    car.hospX = dr["hospX"].ToString();
                    car.hospY = dr["hospY"].ToString();
                    phyInfo.aware = dr["ShenZhi"].ToString();
                    if (!string.IsNullOrEmpty(taskCode))
                    {
                        //获取任务信息
                        StringBuilder taskSB = new StringBuilder(@" 
                                        select  tt.任务编码 , tac.开始受理时刻 ,tac.患者姓名,tac.性别,tac.年龄,tac.联系电话,tac.身份证号码,tac.现场地址 ,tac.主诉 ,
                                        ta.车牌号码 ,ta.车辆编码 , tt.出车时刻 , tt.到达现场时刻 ,tt.离开现场时刻 ,tt.到达医院时刻 ,tt.完成时刻 ,
										tp.工号,tp.姓名,tp.通话号码,gps.经度,gps.纬度
                                        from TTask  tt
                                        left join TAmbulance ta  on tt.车辆编码 = ta.车辆编码
                                        left join TAcceptEvent tac on tt.事件编码=tac.事件编码 and tt.受理序号=tac.受理序号
                                        left join TPerson tp on tt.车辆编码=tp.绑定车辆编码 and tp.类型编码=4
                                        left join TGPSLast gps on tt.车辆编码 =gps.车辆编码
				                        where tt.任务编码='" + taskCode + "'");
                        DataSet taskDS = DataSetUtility.GetDataSetFromMain(taskSB);
                        if (taskDS.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr1 = taskDS.Tables[0].Rows[0];
                            dis.complaint = dr1["主诉"].ToString();
                            dis.callTime = DBConvert.DBTypeToDateTime(dr1["开始受理时刻"]);
                            dis.patientAddress = dr1["现场地址"].ToString();

                            patient.name = dr1["患者姓名"].ToString();
                            patient.gender = dr1["性别"].ToString();
                            patient.age = dr1["年龄"].ToString();
                            patient.telecom = dr1["联系电话"].ToString();
                            patient.idCard = dr1["身份证号码"].ToString();

                            car.carNo = dr1["车牌号码"].ToString();
                            car.carId = dr1["车辆编码"].ToString();
                            car.startTime = DBConvert.DBTypeToDateTime(dr1["出车时刻"]);
                            car.arrivalTime = DBConvert.DBTypeToDateTime(dr1["到达现场时刻"]);
                            car.leaveArrivalTime = DBConvert.DBTypeToDateTime(dr1["离开现场时刻"]);
                            car.toHospTime = DBConvert.DBTypeToDateTime(dr1["到达医院时刻"]);
                            car.compHandTime = DBConvert.DBTypeToDateTime(dr1["完成时刻"]);
                            car.doctor = dr1["姓名"].ToString();
                            car.telphone = dr1["通话号码"].ToString();
                            car.X= dr1["经度"].ToString();
                            car.Y= dr1["纬度"].ToString();

                            if (string.IsNullOrEmpty(dr1["离开现场时刻"].ToString()))
                            {
                                dis.fstHospConTime = string.Empty;
                            }
                            else
                            {
                                dis.fstHospConTime = Convert.ToDateTime(dr1["离开现场时刻"]).ToString("yyyy-MM-dd HH:mm:ss");
                            }


                            string gonghao = dr1["工号"].ToString();
                            //获取设备号
                            CarInfo deviceInfo = DapperData.GetDeviceInfo(gonghao);
                            if (deviceInfo == null)
                            {
                                deviceInfo = new CarInfo();
                            }
                            car.equipCode = deviceInfo.equipCode;
                            car.equipType = deviceInfo.equipType;

                            
                            if (string.IsNullOrEmpty(car.X) || string.IsNullOrEmpty(car.Y) || string.IsNullOrEmpty(car.hospX) || string.IsNullOrEmpty(car.hospY))
                            {
                                car.estiDist = "未获取到坐标，无法计算距离和时间";
                                car.estiTime = "未获取到坐标，无法计算距离和时间";
                            }
                            else
                            {
                                double[] bdArray = CoorConvert.Gps84ToBd09(Convert.ToDouble(car.Y), Convert.ToDouble(car.X));//gps转百度坐标
                                string start = bdArray[0] + "," + bdArray[1];//救护车的纬度和经度凭据
                                                                             //string start = car.Y + "," + car.X;//救护车的经纬度
                                string end = car.hospY + "," + car.hospX;//医院的经纬度
                                                                         //百度算路
                                string postresult = AppConfig.BDApi + "?tactics=11&output=json&origins=" + end + "&destinations=" + start + "&ak=" + AppConfig.BDKey + "&coord_type=wgs84";
                                string result = HttpService.SendDataByGET(postresult);
                                if (!string.IsNullOrEmpty(result))
                                {
                                    BaiduInfo baiduInfo = JsonHelper.DeserializeObject<BaiduInfo>(result);
                                    if (baiduInfo.status == "0")
                                    {
                                        //预估距离
                                        string distance = baiduInfo.result.Select(x => x.distance).FirstOrDefault()?.text;
                                        //预估时间
                                        string duration = baiduInfo.result.Select(x => x.duration).FirstOrDefault()?.text;
                                        car.estiDist = distance;
                                        car.estiTime = duration;
                                    }
                                    else
                                    {
                                        LogUtility.Error("PatientResuorce/GetDisPatientInfo   中百度算路出错", baiduInfo.message);
                                    }
                                }
                            }
                        }

                    }

                    phyInfo.spo2 = dr["spo2"].ToString();
                    phyInfo.temp = dr["temp"].ToString();
                    phyInfo.pulse = dr["pulse"].ToString();
                    phyInfo.rhythm = dr["rhythm"].ToString();
                    blood.diastolic = dr["diastolic"].ToString();
                    blood.systolic = dr["systolic"].ToString();
                    phyInfo.bloodPress = blood;

                    car.toHospital = dr["toHospital"].ToString().Trim();
                    //car.hospX = dr["hospX"].ToString();
                    //car.hospY = dr["hospY"].ToString();
                    //car.estiDist = dr["预估距离"].ToString();
                    //car.estiTime = Convert.ToDateTime(dr["预计到达时间"]).ToString("yyyy-MM-dd HH:mm:ss");

                    dis.patientInfo = patient;
                    dis.phyInfo = phyInfo;
                    dis.carInfo = car;
                    ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    ht.Add("msgId", Guid.NewGuid().ToString());
                    ht.Add("vendorId", "ANKE");
                    ht.Add("disPatientInfo", dis);
                    json = JsonHelper.SerializeObject(ht);
                    LogUtility.Debug("PatientResuorce/GetDisPatientInfo", "调度数据  " + json);
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("PatientResuorce/GetDisPatientInfo", ex.ToString());
            }
            return json;

        }



        public string QueryEcgData(string yqpbch)
        {
            string json = string.Empty;
            try
            {
                Hashtable ht = new Hashtable();
                StringBuilder sb = new StringBuilder(@" select *  from  TDeviceInfo  where   是否有效=1  and  设备编码='" + yqpbch + "'");
                DataSet ds = DataSetUtility.GetDataSetFromTold(sb);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    string personCode = dr["使用人员"].ToString();

                    StringBuilder sb1 = new StringBuilder(@" select   tt.任务编码,tp.工号,tp.绑定车辆编码  from   TTask   tt
                                                             left join TPerson tp  on  tt.车辆编码=tp.绑定车辆编码
					                                         where tt.是否执行中=1  and  tp.工号='" + personCode + "'  ");
                    DataSet ds1 = DataSetUtility.GetDataSetFromMain(sb1);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[0];
                        ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        ht.Add("msgId", Guid.NewGuid().ToString());
                        ht.Add("vendorId", "ANKE");
                        ht.Add("yqpbch", dr["设备编码"].ToString());
                        ht.Add("rwh", dr1["任务编码"].ToString());
                        ht.Add("ecgBeginTime", "");
                        ht.Add("ecgEndTime", "");
                        json = JsonHelper.SerializeObject(ht);
                    }
                    else
                    {
                        EventUtility.OnShowMsg("PatientResuorce/QueryEcgData    未找到人员对应的设备号", true);
                        LogUtility.Error("PatientResuorce/QueryEcgData", " 未找到人员对应的设备号");
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("PatientResuorce/QueryEcgData", ex.ToString());
            }
            return json;
        }


    }
}
